package com.ymatou.iapi.couponservice.testcase;

import java.util.ArrayList;
import java.util.List;



import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.couponservice.parameter.ConversionCouponBean;
import com.ymatou.iapi.couponservice.parameter.getUserUsableCouponQuantityBean;
import com.ymatou.iapi.couponservice.service.getUserUsableCouponQuantityCall;
import com.ymatou.iapi.m.couponservice.parameter.CreateBean;
import com.ymatou.iapi.m.couponservice.parameter.CreateCouponBean;
import com.ymatou.iapi.m.couponservice.parameter.SellerCreateBatchBean;
import com.ymatou.iapi.m.couponservice.service.CreateCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.*;
import com.ymt.utils.tag.*;
import com.ymttest.business.service.CouponServiceCallService;
import com.ymttest.business.service.CouponServiceMCallService;
import com.ymttest.database.model.YmtCouponBatch;
import com.ymttest.database.model.YmtCouponConversionBill;
import com.ymttest.database.model.YmtCouponConversionCode;
import com.ymttest.database.sqlwapper.YmtCouponBatchWapper;
import com.ymttest.database.sqlwapper.YmtCouponConversionBillWapper;
import com.ymttest.database.sqlwapper.YmtCouponConversionCodeWapper;
import com.ymttest.utils.EnvSetup;
import com.ymttest.utils.TestDataManager;
/**
*获取用户可用的优惠券数量
*FreeMaker Template自动生成代码
*/
@TestSuite
public class Ts_getUserUsableCouponQuantity{
	private static getUserUsableCouponQuantityBean getuserusablecouponquantityBean;
	private static getUserUsableCouponQuantityCall getuserusablecouponquantityCall;
	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("获取用户可用的优惠券数量");
	}

	@Before
	public void caseUp() {
		getuserusablecouponquantityBean=new getUserUsableCouponQuantityBean();
		getuserusablecouponquantityCall=new getUserUsableCouponQuantityCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(getuserusablecouponquantityCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getUserUsableCouponQuantity_001() {
		Logger.start(true,"获取用户可用的优惠券数量");
		try {
			int userId = TestDataManager.getRandomIntNum(6);
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();

			String batchCode1 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			couponCallService.receiveCouponInfo(batchCode1, userId);
			
			// 平台券
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			String batchCode2 = couponCallService.createPlatformCoupon(createCouponBean).getBatchCode();
			couponCallService.receiveCouponInfo(batchCode2, userId);

			getuserusablecouponquantityBean.setUserId(userId);
			
			getuserusablecouponquantityCall.setData(getuserusablecouponquantityBean);
			getuserusablecouponquantityCall.callService();			
			
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getStatusCode1(), "验证返回statusCode");
			Logger.verifyEquals("2", getuserusablecouponquantityCall.getQuantity(), "验证返回Quantity");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	@Ignore
	public void Tc_getUserUsableCouponQuantity_002() {
		Logger.start(true,"UserId空");
		try {
			getuserusablecouponquantityCall.setData(getuserusablecouponquantityBean);
			getuserusablecouponquantityCall.callService();			
			
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getStatusCode1(), "验证返回statusCode");
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getQuantity(), "验证返回Quantity");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getUserUsableCouponQuantity_003() {
		Logger.start(true,"UserId不存在");
		try {
			int userId = TestDataManager.getRandomIntNum(6);
			getuserusablecouponquantityBean.setUserId(userId);

			getuserusablecouponquantityCall.setData(getuserusablecouponquantityBean);
			getuserusablecouponquantityCall.callService();			
			
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getStatusCode1(), "验证返回statusCode");
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getQuantity(), "验证返回Quantity");
		
		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getUserUsableCouponQuantity_004() {
		Logger.start(true,"获取用户可用的优惠券数量-商家券");
		try {
			int userId = TestDataManager.getRandomIntNum(6);
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();	

			for (int i = 0; i < 5; i++) {
	
			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();

			String batchCode1 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			couponCallService.receiveCouponInfo(batchCode1, userId);
			}
			getuserusablecouponquantityBean.setUserId(userId);
			
			getuserusablecouponquantityCall.setData(getuserusablecouponquantityBean);
			getuserusablecouponquantityCall.callService();			
			
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getStatusCode1(), "验证返回statusCode");
			Logger.verifyEquals("5", getuserusablecouponquantityCall.getQuantity(), "验证返回Quantity");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getUserUsableCouponQuantity_005() {
		Logger.start(true,"获取用户可用的优惠券数量-平台领取券");
		try {
			int userId = TestDataManager.getRandomIntNum(6);
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();	
			for (int i = 0; i < 10; i++) {
					
			// 平台券
			CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForRecieve();
			String batchCode2 = couponCallService.createPlatformCoupon(createCouponBean).getBatchCode();
			couponCallService.receiveCouponInfo(batchCode2, userId);
			}
			
			getuserusablecouponquantityBean.setUserId(userId);
			
			getuserusablecouponquantityCall.setData(getuserusablecouponquantityBean);
			getuserusablecouponquantityCall.callService();			
			
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getStatusCode1(), "验证返回statusCode");
			Logger.verifyEquals("10", getuserusablecouponquantityCall.getQuantity(), "验证返回Quantity");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_getUserUsableCouponQuantity_006() {
		Logger.start(true,"获取用户可用的优惠券数量-兑换券");
		try {
			int userId = TestDataManager.getRandomIntNum(6);
			CouponServiceMCallService couponCallService = new CouponServiceMCallService();	

			// 商家优惠券
			SellerCreateBatchBean couponBatchBean = couponCallService.setDefaultSellerCreateBatchBean();

			String batchCode1 = couponCallService.SellerCreateBatch(couponBatchBean).getBatchCode();
			couponCallService.receiveCouponInfo(batchCode1, userId);
			
			// 平台券
						CreateCouponBean createCouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForRecieve();
						String batchCode2 = couponCallService.createPlatformCoupon(createCouponBean).getBatchCode();
						couponCallService.receiveCouponInfo(batchCode2, userId);
			

			// 平台兑换优惠券
			CouponServiceCallService couponServiceCallService = new CouponServiceCallService();

				// createcouponBean
			CreateCouponBean createcouponBean = couponCallService.setDefaultPlatformCreateCouponBeanForConversion();

				String batchcode2 = couponCallService.createPlatformCoupon(createcouponBean).getBatchCode();
				List<YmtCouponBatch> ymtCouponBatchs = new YmtCouponBatchWapper().selectBysBatchCode(batchcode2);

					List<String> batchCodes = new ArrayList<>();
					batchCodes.add(batchcode2);

					CreateBean createBean = couponCallService.setDefaultRandomCreateBean(batchCodes);
					CreateCall createCall = couponCallService.create(createBean);
				
					// 等待兑换码生成
					YmtCouponConversionBill bill = new YmtCouponConversionBillWapper()
							.selectByBillName(createBean.getBillName()).get(0);
					List<YmtCouponConversionCode> ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());
					
					int flag=0;
					while(ymtCouponConversionCodes.size()<createBean.getGenerateQuantity() && flag<500)
					{
						flag++;
						Thread.sleep(200);
						ymtCouponConversionCodes = new YmtCouponConversionCodeWapper().selectByConversionBillId(bill.getConversionbillid());	
					}
					Logger.verifyEquals(createBean.getGenerateQuantity(), ymtCouponConversionCodes.size(), "验证数据Ymt_CouponConversionCode中生成相应记录");
					
					ConversionCouponBean conversionCouponBean = couponServiceCallService.setConversionCouponBean(userId,ymtCouponConversionCodes.get(0).getConversioncode());
					
					String batchCode3 = couponServiceCallService.ConversionCoupon(conversionCouponBean).getCouponCodes().get(0);
				
			couponCallService.receiveCouponInfo(batchCode3, userId);

			getuserusablecouponquantityBean.setUserId(userId);
			
			getuserusablecouponquantityCall.setData(getuserusablecouponquantityBean);
			getuserusablecouponquantityCall.callService();			
			
			Logger.verifyEquals("0", getuserusablecouponquantityCall.getStatusCode1(), "验证返回statusCode");
			Logger.verifyEquals("3", getuserusablecouponquantityCall.getQuantity(), "验证返回Quantity");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}